package com.hyg.lib_music.ui.activity;

import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.CompoundButton;

import com.alibaba.fastjson.JSONObject;
import com.hyg.lib_base.mainUtils.StatusBarUtil;
import com.hyg.lib_common.Base.BaseActivity;
import com.hyg.lib_common.DataModel.YangShengInfo.ReceiveDataInt;
import com.hyg.lib_common.Net.CommonHttpUtil;
import com.hyg.lib_common.sweetalertdialog.SweetAlertDialog;
import com.hyg.lib_music.R;
import com.hyg.lib_music.databinding.ActivityTreatmentFinishBinding;

import java.io.IOException;
import java.util.ArrayList;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class TreatmentFinishActivity extends BaseActivity {

    ActivityTreatmentFinishBinding binding;
    int feeling = 3;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityTreatmentFinishBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        init();
    }

    public void init() {
        StatusBarUtil.setStatusBar(1, this, getResources().getColor(R.color.white), 60, null, null, true);
        binding.back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        binding.lnResultBad.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                binding.lnResultBad.setBackground(getDrawable(R.drawable.cor_main_solid_small));
                binding.lnResultCommon.setBackground(getDrawable(R.drawable.cor_gray_solid));
                binding.lnResultGood.setBackground(getDrawable(R.drawable.cor_gray_solid));
                binding.lnResultBest.setBackground(getDrawable(R.drawable.cor_gray_solid));

                binding.tvResultBad.setTextColor(getResources().getColor(R.color.white));
                binding.tvResultCommon.setTextColor(getResources().getColor(R.color.musicTextHint));
                binding.tvResultGood.setTextColor(getResources().getColor(R.color.musicTextHint));
                binding.tvResultBest.setTextColor(getResources().getColor(R.color.musicTextHint));
                feeling = 1;
            }
        });
        binding.lnResultCommon.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                binding.lnResultBad.setBackground(getDrawable(R.drawable.cor_gray_solid));
                binding.lnResultCommon.setBackground(getDrawable(R.drawable.cor_main_solid_small));
                binding.lnResultGood.setBackground(getDrawable(R.drawable.cor_gray_solid));
                binding.lnResultBest.setBackground(getDrawable(R.drawable.cor_gray_solid));
                binding.tvResultBad.setTextColor(getResources().getColor(R.color.musicTextHint));
                binding.tvResultCommon.setTextColor(getResources().getColor(R.color.white));
                binding.tvResultGood.setTextColor(getResources().getColor(R.color.musicTextHint));
                binding.tvResultBest.setTextColor(getResources().getColor(R.color.musicTextHint));
                feeling = 2;
            }
        });
        binding.lnResultGood.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                binding.lnResultBad.setBackground(getResources().getDrawable(R.drawable.cor_gray_solid));
                binding.lnResultCommon.setBackground(getResources().getDrawable(R.drawable.cor_gray_solid));
                binding.lnResultGood.setBackground(getResources().getDrawable(R.drawable.cor_main_solid_small));
                binding.lnResultBest.setBackground(getResources().getDrawable(R.drawable.cor_gray_solid));
                binding.tvResultBad.setTextColor(getResources().getColor(R.color.musicTextHint));
                binding.tvResultCommon.setTextColor(getResources().getColor(R.color.musicTextHint));
                binding.tvResultGood.setTextColor(getResources().getColor(R.color.white));
                binding.tvResultBest.setTextColor(getResources().getColor(R.color.musicTextHint));
                feeling = 3;
            }
        });
        binding.lnResultBest.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                binding.lnResultBad.setBackground(getResources().getDrawable(R.drawable.cor_gray_solid));
                binding.lnResultCommon.setBackground(getResources().getDrawable(R.drawable.cor_gray_solid));
                binding.lnResultGood.setBackground(getResources().getDrawable(R.drawable.cor_gray_solid));
                binding.lnResultBest.setBackground(getResources().getDrawable(R.drawable.cor_main_solid_small));

                binding.tvResultBad.setTextColor(getResources().getColor(R.color.musicTextHint));
                binding.tvResultCommon.setTextColor(getResources().getColor(R.color.musicTextHint));
                binding.tvResultGood.setTextColor(getResources().getColor(R.color.musicTextHint));
                binding.tvResultBest.setTextColor(getResources().getColor(R.color.white));
                feeling = 4;
            }
        });

        binding.btnSubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ArrayList<Integer> list = new ArrayList<>();
                if (binding.cbItem1.isChecked()) {
                    list.add(1);
                }
                if (binding.cbItem2.isChecked()) {
                    list.add(2);
                }
                if (binding.cbItem3.isChecked()) {
                    list.add(3);
                }
                if (binding.cbItem4.isChecked()) {
                    list.add(4);
                }
                if (binding.cbItem5.isChecked()) {
                    list.add(5);
                }
                if (binding.cbItem6.isChecked()) {
                    list.add(6);
                }
                if (binding.cbItem7.isChecked()) {
                    list.add(7);
                }
                updataInfo(list);
            }
        });
    }

    private void updataInfo(ArrayList<Integer> list) {
        CommonHttpUtil.updateCourceResultInfo(TreatmentFinishActivity.this, feeling, list.toString(),
                binding.etAdviceDetail.getText().toString(), new Callback() {
                    @Override
                    public void onFailure(Call call, IOException e) {
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                ErrorDialog("服务器错误");
                            }
                        });
                    }

                    @Override
                    public void onResponse(Call call, Response response) throws IOException {
                        ReceiveDataInt dataInt = JSONObject.parseObject(response.body().string(), ReceiveDataInt.class);
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                if (dataInt.getCode() == 200) {
                                    showSuccessDialog();
                                } else {
                                    ErrorDialog(dataInt.getMessage());
                                }
                            }
                        });
                    }
                });
    }

    public void showSuccessDialog() {
        if (pDialog.isShowing())
            pDialog.dismiss();
        pDialog = new SweetAlertDialog(TreatmentFinishActivity.this, SweetAlertDialog.NORMAL_TYPE);
        pDialog.setTitleText("信息提交成功！");  //(/n自动清除本机报告数据)
        pDialog.setConfirmButton("好的", new SweetAlertDialog.OnSweetClickListener() {
            public void onClick(SweetAlertDialog sweetAlertDialog) {
                sweetAlertDialog.dismiss();
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        finish();
                    }
                }, 300);
            }
        });
        pDialog.show();
    }



}